import { getWorkFlowByType } from '@/pages/WorkFlow/utils';
import { saveApplyForm } from '@/pages/WorkFlow/services';

/**
 * 流程类型常量
 * @type {{SALARY_TEMPLATE_APPLY: string}}
 */
const PROCESS_TYPE = {
    /* 财务月模板流程 */
    SALARY_TEMPLATE_APPLY: 'salaryTemplateApply',
    /* 财务发放设置流程 */
    SALARY_SET_GRANT_APPLY: 'salarySetGrantApply',
    /* 财务险金档案流程 */
    SALARY_SET_SOCIAL_APPLY: 'SalarySetSocialApply',
    /* 财务个税档案流程 */
    SALARY_SET_TAX_APPLY: 'SalarySetTaxApply',
    /* 复核薪资提交审批流程 */
    SALARY_RECHECK_APPLY: 'SalaryRecheckApply',
    /* 财务cpf档案流程 */
    SALARY_SET_CPF_APPLY: 'SalarySetCpfApply',
    /* 财务sdl档案流程 */
    SALARY_SET_SDL_APPLY: 'SalarySetSdlApply',
    /* 财务shg档案流程 */
    SALARY_SET_SHG_APPLY: 'SalarySetShgApply'
};

/**
 * 发起流程
 * @param processType 流程类型
 * @param postData 提交的数据
 * @returns {Promise<any|Promise<{data: *}>|undefined>}
 */
const submitProcess = async function submitProcess(processType, postData) {
    const user = localStorage.getItem('user');
    const { employeeCode, employeeName } = user ? JSON.parse(user) : {};

    // 检查流程是否存在
    const workFlowInfo = await getWorkFlowByType(processType, true);

    if (!workFlowInfo) {
        return undefined;
    }

    const processData = {
        // 提交表单数据
        formJson: JSON.stringify(postData),
        // 流程id
        processDefinitionId: workFlowInfo.actProcessDefinitionId,
        // 流程名称
        applicationName: `${workFlowInfo.processName} - ${employeeName}`,
        // 员工编码
        employeeCode,
        // 备注：
        applicationAbstract: workFlowInfo.processName
    };

    return saveApplyForm(processData);
};

export { PROCESS_TYPE, submitProcess, getWorkFlowByType };
